<?php
declare(strict_types=1);

namespace lib\data;

class Shift extends \lib\sync\SyncableData {

    protected static string $SyncKey = 'Shift';
    protected static string $TableName = 'tp_nb_goods_shift';

    public int $id;                         # 主键
    public int $operator_uid;               # 操作人ID(是谁操作的交接班)
    public int $uid;                        # 当班人ID
    public int $mid;                        # 商户ID
    public int $last_id;                    # 上一班ID
    public string $no;                      # 交班单号

    # 接班数据
    public float $amount;                   # [现金]接班金额
    public float $amount_online;            # [线上]接班金额

    # 交班数据
    public float $last_amount;              # [现金]交班金额
    public float $last_amount_online;       # [线上]交班金额

    # 收入数据(汇总)
    public float $amount_in;                # [现金]营收金额
    public float $amount_online_in;         # [线上]营收金额
    public float $amount_member_in;         # [会员]营收金额

    # 支出数据(汇总)
    public float $amount_out;               # [现金]支出金额
    public float $amount_online_out;        # [线上]支出金额
    public float $amount_member_out;        # [会员]支出金额

    # 销售数据
    public float $amount_sale;              # [现金]销售金额
    public float $amount_online_sale;       # [线上]销售金额
    public float $amount_member_sale;       # [会员]销售金额

    # 退款数据
    public float $amount_refund;            # [现金]退款金额
    public float $amount_online_refund;     # [线上]退款金额
    public float $amount_member_refund;     # [会员]退款金额

    # 进货支出数据
    public float $amount_purchase;          # [现金]进货金额
    public float $amount_online_purchase;   # [线上]进货金额

    # 出货收入数据
    public float $amount_sell;              # [现金]出货金额
    public float $amount_online_sell;       # [线上]出货金额

    # 库存盘点盈亏数据
    public float $amount_stock_in;          # [现金]库存盈亏金额
    public float $amount_stock_out;         # [线上]库存盈亏金额

    # 手续费支出
    public float $amount_fee;               # [线上]手续费金额

    # 钱箱提现总额
    public float $cash_out;                 # [现金]钱箱提现总额

    # 线上提现总额
    public float $online_out;               # [线上]线上提现总额

    public int $starttime;                  # 接班开始时间
    public int $endtime;
    public int $createtime;

    protected static array $propNamesC = ['id', 'operator_uid', 'uid','mid','last_id','no','amount','amount_online','last_amount','last_amount_online','amount_in','amount_online_in','amount_member_in','amount_out','amount_online_out','amount_member_out','amount_sale','amount_online_sale','amount_member_sale','amount_refund','amount_online_refund','amount_member_refund','amount_purchase','amount_online_purchase','amount_sell','amount_online_sell','amount_stock_in','amount_stock_out','amount_fee', 'cash_out', 'online_out', 'starttime','endtime','createtime'];
    protected static array $propNamesU = ['last_amount','last_amount_online','amount_in','amount_online_in','amount_member_in','amount_out','amount_online_out','amount_member_out','amount_sale','amount_online_sale','amount_member_sale','amount_refund','amount_online_refund','amount_member_refund','amount_purchase','amount_online_purchase','amount_sell','amount_online_sell','amount_stock_in','amount_stock_out','amount_fee','cash_out', 'online_out', 'endtime'];

    protected static $srcKey = ['','online_','member_'];

    # 销售
    public function onSale(int $src, float $amount, float $fee = 0, array &$oldValue) :?array {
        $amount_in = 'amount_'.static::$srcKey[$src].'in';
        $amount_sale = 'amount_'.static::$srcKey[$src].'sale';

        $update = [
            $amount_in=>$this->$amount_in + $amount,        # 增加收入汇总
            $amount_sale=>$this->$amount_sale + $amount,    # 增加销售收入
        ];
        
        if(abs($fee) > 0.0000999 && $src == 1){
            $update['amount_fee'] = $this->amount_fee + $fee;                         # 增加服务费
        }
        echo json_encode($update, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        if(null !== ($changed = $this->update($update, false, $oldValue))){
            return $changed;
        }
        return null;
    }

    # 退款
    public function onRefund(int $src, float $amount, float $fee = 0, array &$oldValue) :array {
        $amount_in = 'amount_'.static::$srcKey[$src].'in';
        $amount_refund = 'amount_'.static::$srcKey[$src].'refund';
        $update = [
            $amount_in=>$this->$amount_in - $amount,          # 减少收入汇总
            $amount_refund=>$this->$amount_refund + $amount,  # 增加退款汇总
        ];
        if(abs($fee) >= 0.0000999){
            $update['amount_fee'] = $this->amount_fee - $fee;                         # 减少服务费
        }
        if(null !== ($changed = $this->update($update, false, $oldValue))){
            return $changed;
        }
        return null;
    }

    # 进货
    public function onPurchase(int $src, float $amount, float $fee = 0, array &$oldValue) :array {
        $amount_out = 'amount_'.static::$srcKey[$src].'out';
        $amount_purchase = 'amount_'.static::$srcKey[$src].'purchase';
        $update = [
            $amount_out=>$this->$amount_out + $amount,              # 增加支出汇总
            $amount_purchase=>$this->$amount_purchase + $amount,    # 增加进货支出
        ];

        if(abs($fee) >= .0000999){
            $update['amount_fee'] = $this->amount_fee - $fee;                         # 减少服务费
        }

        if(null !== ($changed = $this->update($update, false, $oldValue))){
            return $changed;
        }
        return null;
    }

    # 出货
    public function onSell(int $src, float $amount, float $fee, array &$oldValue) :array {
        $amount_in = 'amount_'.static::$srcKey[$src].'in';
        $amount_sell = 'amount_'.static::$srcKey[$src].'sell';
        $update = [
            $amount_in=>$this->$amount_in + $amount,              # 增加收入汇总
            $amount_sell=>$this->$amount_sell + $amount,          # 增加出货收入
        ];
        if(abs($fee) >= 0.0000999){
            $update['amount_fee'] = $this->amount_fee - $fee;                         # 减少服务费
        }
        if(null !== ($changed = $this->update($update, false, $oldValue))){
            return $changed;
        }
        return null;
    }

    # 提现
    public function onCashout(float $amount, float $online_amount, float $fee_amount, int $endtime, array &$oldValue) :array {
        $update = [];
        if($amount > 0.0000999){
            $update = [
                'amount_out'=>$this->amount_out + $amount,              # 增加支出汇总
                'cash_out' => $this->cash_out + $amount,                # 增加钱箱提现总额
            ];
        }
        if($online_amount > 0.0000999){
            $update['amount_online_out']= $this->amount_online_out + $online_amount;              # 增加支出汇总
            $update['online_out'] =  $this->online_out + $online_amount;                          # 增加线上提现总额
        }

        if($fee_amount > 0.0000999){
            $update['amount_fee'] = $this->amount_fee - $fee_amount;                         # 减少服务费
        }
          
        # 填写交班时间
        if($endtime > 0){
            $update['endtime'] = $endtime;
        }

        if(null !== ($changed = $this->update($update, false, $oldValue))){
            return $changed;
        }
        return null;
    }

    # 库存盘点
    public function onStock(float $amount_in, $amount_out, array &$oldValue) :?array {
        if($amount_in > 0.0000999){
           $update['amount_stock_in'] = $amount_in;
        }
        if($amount_out > 0.0000999){
            $update['amount_stock_out'] = $amount_out;
        }

        # 执行更新
        if(isset($update) && null !== ($changed = $this->update($update, false, $oldValue))){
            return $changed;
        }
        return null;
    }
}